package com.android.example.app3;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class ScanRecordActivity extends AppCompatActivity {

    private RuleFileManager ruleFileManager;
    private WebView webView;
    private String key_string = "photoList";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_scan_record);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);

            // 初始化管理器
            ruleFileManager = new RuleFileManager(this);
            // 获取传递过来的Bundle
            key_string = getIntent().getStringExtra("key_string");


            webView = findViewById(R.id.webView);
            WebSettings webSettings = webView.getSettings();
            webSettings.setAllowContentAccess(true); // 允许内容访问，包括粘贴板
            webSettings.setAllowFileAccess(true); // 允许文件访问
// 以下设置是可选的，但可以增强复制功能
            webView.setLongClickable(true); // 确保长按可用
            webView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    // 默认行为是显示上下文菜单（包含复制等），所以返回false表示不消费事件，继续传递
                    return false;
                }
            });
            webView.setWebViewClient(new WebViewClient()); // 可以自定义WebViewClient
//        ruleFileManager.addRuleToFile("New Rule", "BlockList");
            ruleFileManager.displayRulesFileInWebView(webView, key_string);



            return insets;
        });
    }
}